package com.zpp;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.Base64.Encoder;

/**
 * @ClassName: FileTest
 * @Author:zengpengpeng
 * @Date 2022/2/17
 */
@Slf4j
public class FileTest {
    /**
     * 读取目标文件路径
     */
    public static final String FILE_PATH = "D:\\backup.txt";

    /**
     * main方法
     * @param args
     */
    public static void main(String[] args) {
        readOnlyTxtFile();
        readLineTxtFile();
    }

    /**
     * 读取文件的方法：一个字节读取
     */
    public static void readOnlyTxtFile() {
        File file;
        try {
            InputStream inputStream = new FileInputStream(FILE_PATH);
            int i = 0;
            while ((i = inputStream.read()) != -1) {
                log.debug((char) i + " ");
            }
        } catch (IOException e) {
            log.debug("读取失败："+e);
        }
    }

    /**
     * 读取文件方法:一行一行读取
     */
    public static void readLineTxtFile() {
        File file = new File(FILE_PATH);
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String line = null;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            log.debug("读取失败：" + e);
        }
    }
}
